package com.longxi.tianjintianbao.ui.presenter.approval;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.listener.SelectTaskListListener;
import com.longxi.tianjintianbao.model.approval.Approval;
import com.longxi.tianjintianbao.model.approval.Approval2;
import com.longxi.tianjintianbao.model.task.Task;
import com.longxi.tianjintianbao.ui.activity.approval.ApprovalDetailActivity;
import com.longxi.tianjintianbao.ui.activity.task.TaskDetailActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.TaskListViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.approval.ApprovalViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.base.LazyFragment;
import com.longxi.tianjintianbao.ui.view.approval.IApprovalListFgView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ApprovalListFgPresenter extends BasePresenter<IApprovalListFgView> {

    private MultiTypeAdapter adapter;
    private Items items;

    private String mType = "";

    public ApprovalListFgPresenter(BaseActivity context, LazyFragment fragment) {
        super(context, fragment);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(Approval.class, new ApprovalViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void getTaskList(int page, String state){
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("type", state);
            object.put("page", page);
            object.put("rowcount", 10);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().myApproval(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<Approval>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<Approval> result) {
                items = NListRefresh.getInstance().fjData(result,
                        getView().getRefreshLayout(),
                        items,
                        adapter,
                        mFragment);
            }
        });
    }

    AdapterListener listener = new AdapterListener() {

        @Override
        public void onClick(int position) {
            Approval approval = (Approval) items.get(position);
            Intent intent = new Intent(mContext, ApprovalDetailActivity.class);
            intent.putExtra(AppConst.APPROVAL, approval);
            mFragment.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_APPROVAL_DETAIL);
        }

        @Override
        public void onDelete(int position) {

        }
    };

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != mContext.RESULT_OK){
            return;
        }
        switch (requestCode) {
            case AppConst.IntentRequstCode.ACTIVITY_APPROVAL_DETAIL:
                getView().getRefreshLayout().autoRefresh();
                break;
        }
    }

    @Override
    public void detachView() {
        super.detachView();
        this.listener = null;
        this.adapter = null;
        this.items = null;
    }
}
